package com.pig4cloud.pig.farm.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.farm.entity.FarmGreenhouseEntity;
import com.pig4cloud.pig.farm.service.FarmGreenhouseService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 农业大棚
 *
 * @author lty
 * @date 2024-11-04 20:53:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/farmGreenhouse" )
@Tag(description = "farmGreenhouse" , name = "农业大棚管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class FarmGreenhouseController {

    private final  FarmGreenhouseService farmGreenhouseService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param farmGreenhouse 农业大棚
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('farm_farmGreenhouse_view')" )
    public R getFarmGreenhousePage(@ParameterObject Page page, @ParameterObject FarmGreenhouseEntity farmGreenhouse) {
        LambdaQueryWrapper<FarmGreenhouseEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(farmGreenhouse.getHouseName()),FarmGreenhouseEntity::getHouseName,farmGreenhouse.getHouseName());
		wrapper.eq(Objects.nonNull(farmGreenhouse.getBaseinfoId()),FarmGreenhouseEntity::getBaseinfoId,farmGreenhouse.getBaseinfoId());
        return R.ok(farmGreenhouseService.page(page, wrapper));
    }


    /**
     * 通过id查询农业大棚
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('farm_farmGreenhouse_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(farmGreenhouseService.getById(id));
    }

    /**
     * 新增农业大棚
     * @param farmGreenhouse 农业大棚
     * @return R
     */
    @Operation(summary = "新增农业大棚" , description = "新增农业大棚" )
    @SysLog("新增农业大棚" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('farm_farmGreenhouse_add')" )
    public R save(@RequestBody FarmGreenhouseEntity farmGreenhouse) {
        return R.ok(farmGreenhouseService.save(farmGreenhouse));
    }

    /**
     * 修改农业大棚
     * @param farmGreenhouse 农业大棚
     * @return R
     */
    @Operation(summary = "修改农业大棚" , description = "修改农业大棚" )
    @SysLog("修改农业大棚" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('farm_farmGreenhouse_edit')" )
    public R updateById(@RequestBody FarmGreenhouseEntity farmGreenhouse) {
        return R.ok(farmGreenhouseService.updateById(farmGreenhouse));
    }

    /**
     * 通过id删除农业大棚
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除农业大棚" , description = "通过id删除农业大棚" )
    @SysLog("通过id删除农业大棚" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('farm_farmGreenhouse_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(farmGreenhouseService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param farmGreenhouse 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('farm_farmGreenhouse_export')" )
    public List<FarmGreenhouseEntity> export(FarmGreenhouseEntity farmGreenhouse,Long[] ids) {
        return farmGreenhouseService.list(Wrappers.lambdaQuery(farmGreenhouse).in(ArrayUtil.isNotEmpty(ids), FarmGreenhouseEntity::getId, ids));
    }
}